<?php
namespace app\admin\service;
use app\model\User;
use app\model\UserType;
use app\model\Cash;

class UserService{
    public function index($param)
    {
        $userType = $this->userType();
        $userModel = new User;
        if(!array_key_exists("key",$param)){
            $key = '';
        }else{
            $key = $param['key'];
        }
        if(array_key_exists('user_type_id',$param)){//判断是否选择了用户类型
            if($param['user_type_id'] == 0){
                $user_type_id = implode(',',array_column($userType,'id'));
            }else{
                $user_type_id = $param['user_type_id'];;
            }
            $type_id = $param['user_type_id'];
        }else{
            $user_type_id = implode(',',array_column($userType,'id'));
            $type_id = 0;
        }
        // dump($user_type_id);die;
        if(!$key){//判断是否输入了用户名或者手机号
            $users = $userModel::with([
                'userType' => function($query){
                    $query->field('id,name');
                }
            ])->where('user_type_id','in',$user_type_id)->order('add_time desc')->paginate(10,false,['query'=>$param]);
        }else{
            $users = $userModel::with([
                'userType' => function($query){
                    $query->field('id,name');
                }
            ])->where('username|mobile','like','%'.$key.'%')->where('user_type_id','in',$user_type_id)->order('add_time desc')->paginate(10,false,['query'=>$param]);
        }
        $page = $users->render();
        return ['users'=>$users,'page'=>$page,'key'=>$key,'userType'=>$userType,'type_id'=>$type_id];
    }

    public function userType()
    {
        $userTypeModle = new UserType;
        $userTypes = $userTypeModle::all();
        return $userTypes;
    }

    public function addUserType($post,$type = 1)//$type:1代表新增，2代表编辑
    {
        if(!$post['name']){
            return ['code'=>0,'msg'=>'请输入用户分类的名称'];
        }
        $post['add_time'] = time();
        $userTypeModel = new UserType;
        if($type == 1){
            $info = $userTypeModel::insert($post);
            if(!$info){
                return ['code'=>-1,'msg'=>'系统繁忙'];
            }
            return ['code'=>1,'msg'=>'新增成功'];
        }else{
            $id = $post['id'];
            unset($post['id']);
            $info = $userTypeModel::where('id',$id)->update($post);
            if(!$info){
                return ['code'=>-1,'msg'=>'系统繁忙'];
            }
            return ['code'=>1,'msg'=>'编辑成功'];
        }
    }

    public function editUserTypeGet($id)
    {
        $userTypeModel = new UserType;
        $data = $userTypeModel::get($id);
        return $data;
    }

    public function delUserType($id)
    {
        $userTypeModel = new UserType;
        $info = $userTypeModel::where('id',$id)->delete();
        if(!$info){
            return ['code'=>0,'msg'=>'系统繁忙'];
        }
        return ['code'=>1,'msg'=>'删除成功'];
    }

    public function cash($param,$where = '1 = 1')
    {
        $cashModel = new Cash;
        $cashes = $cashModel::with('user,bankcard')->where($where)->order('add_time desc')->paginate(10,false,['query'=>$param]);
        // dump($cashes);die;
        $page = $cashes->render();
        return ['cashes'=>$cashes,'page'=>$page];
    }

    public function cashExamine($post)
    {
        $cashModel = new Cash;
        $userModel = new User;
        $cash = $cashModel::with('user')->find($post['id']);
        if($post['status'] == 2){
            $info_user = $userModel::where('id',$cash->uid)->setInc('cash',$cash->account);
            if(!$info_user){
                return ['code'=>-1,'msg'=>'系统繁忙'];
            }
            $info_cash = $cashModel::where('id',$post['id'])->update(['status'=>2]);
            if(!$info_cash){
                $userModel::where('id',$cash->uid)->setDec('cash',$cash->account);
                return ['code'=>-2,'msg'=>'系统繁忙'];
            }
        }else{
            $info_user = $userModel::where('id',$cash->uid)->setInc('balance',$cash->account);
            if(!$info_user){
                return ['code'=>-1,'msg'=>'系统繁忙'];
            }
            $info_cash = $cashModel::where('id',$post['id'])->update(['status'=>3]);
            if(!$info_cash){
                $userModel::where('id',$cash->uid)->setDec('cash',$cash->account);
                return ['code'=>-2,'msg'=>'系统繁忙'];
            }
        }
        return ['code'=>'1','msg'=>'已审核'];
    }
}
